package ipfms.stripes.util;

import javax.mail.*;
import java.util.Properties;
import javax.mail.internet.*;
import com.google.inject.Inject;
import org.apache.log4j.Logger;
import javax.mail.Authenticator;
import ipfms.stripes.BaseActionBean;
import javax.mail.PasswordAuthentication;


public class SendMail extends BaseActionBean {

    private static final Logger logger = Logger.getLogger(SendMail.class);
    @Inject
    private Configuration conf;

    public void mail(String con) {
        logger.info("inside mail : START");

        Properties props = new Properties();

        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.host", "mail.theta-edge.com");
        props.put("mail.smtp.auth", "true");

        Authenticator auth = new javax.mail.Authenticator() {

            public PasswordAuthentication getPasswordAuthentication() {
                String username = "abdul.hakim";
                String password = conf.getProperty("mail.pwd");
                return new PasswordAuthentication(username, password);
            }
        };
        javax.mail.Session mailSession = javax.mail.Session.getDefaultInstance(props, auth);
        // uncomment for debugging infos to stdout
        // mailSession.setDebug(true);
        Transport transport;
        try {
            transport = mailSession.getTransport();
            MimeMessage message = new MimeMessage(mailSession);
            message.setSubject("THv3");
            message.setContent(con, "text/plain");
            message.setFrom(
                    new InternetAddress("abdul.hakim@theta-edge.com"));
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress("haqqiem@gmail.com"));
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress("hakim@etanahjv.com.my"));
            transport.connect();
            transport.sendMessage(message,
                    message.getRecipients(Message.RecipientType.TO));
            transport.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        logger.info("inside mail: FINISH");
    }
}

